<template>
  <div>
    <vxe-grid v-bind="gridOptions" ref="xTable"></vxe-grid>
  </div>
</template>

<script>
  export default {
    props: ['exportData'],
    data() {
      return {
        tableData: [],
        personInfo: '',
        schoolAndUsage: '',
        titleValue: '',
        align: 'center',
        detail: '',
        complete: false,
        gridOptions: {
          border: true,
          resizable: true,
          align: 'left',
          toolbarConfig: {
          },
          columns: [],
          data: []
        }
      }
    },
    watch: {
      exportData() {
        this.render()
      }
    },
    methods: {
      exportDataEvent(name) {
        this.$refs.xTable.exportData({
          filename: this.detail||name,
          sheetName: 'Sheet1',
          isHeader: true,
          isFooter: true,
          data: this.defailtMatchDataTable
        })
      },
      genDateStr(date, separator) {
        if (date == 'Invalid Date') return "无效时间格式"
        let dateStr = this.$moment(date).format('YYYY-MM-DD')
        return dateStr
      },

      updateMessage: function() {
        this.$nextTick(function() {
          this.exportDataEvent()
        })
      },
      render: function() {
        let contrastData = this.exportData
        let date = this.$moment(contrastData.date).format('YYYY-MM')
        let memo = contrastData.memo;
        let onePerson = contrastData.playerDatas[0];
        let eachBirthday = new Date(onePerson.birthday);
        let eachYear = eachBirthday.getFullYear();
        let eachTime = new Date(onePerson.time);
        let age = eachTime.getFullYear() - eachYear;
        let usageName = contrastData.usage;
        let time = new Date(onePerson.time);
        let schoolName = contrastData.school;
        let onePersonDatas = contrastData.playerDatas[0].datas;
        let detail = usageName + '\n' + schoolName + '\n' + '测量日期：' + this.genDateStr(time, "-") + '\n运动大项：' +
          onePerson.sport + '\n' + onePerson.teacher + '\n';
        this.detail = detail
        let peopleDatasInfo = contrastData.playerDatas;
        let metaInfo = contrastData.meta;
        let personInfoFilter = onePerson.playerName + ' - ' + onePerson.gender + ' - ' + age + '岁'
        this.personInfo = personInfoFilter
        this.schoolAndUsage = contrastData.usage + ' - ' + contrastData.school
        this.titleValue = detail
        var tableDataFilter = []
        let newColumns = [
          {field: 'date', title: '日期'},
          {
            field: 'meta',
            title: '指标名称'
          },
        ];
        let newDatasSpan = [];
        let rowMetaIds = [];
        for (let i in metaInfo) {
          let rowData = {};
          rowData.meta = Object.values(metaInfo[i])[0];
          newDatasSpan.push(rowData)
          rowMetaIds.push(Object.keys(metaInfo[i])[0])
        }
        for (let i = 0; i < rowMetaIds.length; i++) {
          for (let j in peopleDatasInfo) {
            newDatasSpan[i]['date'] = date
            if (peopleDatasInfo[j]["datas"][rowMetaIds[i]]) {
              if (Object.values(peopleDatasInfo[j]["datas"][rowMetaIds[i]])[0][0]['value'] == '未找到用于计算的基础数据') {
                newDatasSpan[i]["p" + j] = Object.values(peopleDatasInfo[j]["datas"][rowMetaIds[i]])[0][0].value
              } else {
                newDatasSpan[i]["p" + j] = Object.values(peopleDatasInfo[j]["datas"][rowMetaIds[i]])[0][0].value +
                  Object.values(peopleDatasInfo[j]["datas"][rowMetaIds[i]])[0][0].unit
              }

            } else {
              newDatasSpan[i]["p" + j] = "-"
            }
          }
        }

        for (let i in peopleDatasInfo) {
          let listColumns = {}
          let listTableData = {}
          let age = new Date(peopleDatasInfo[i].time).getFullYear() - new Date(peopleDatasInfo[i].birthday)
            .getFullYear();
          let personInfoFilter = peopleDatasInfo[i].playerName + ' - ' + peopleDatasInfo[i].gender + ' - ' + age + '岁'

          listColumns.title = personInfoFilter
          listColumns.field = peopleDatasInfo[i].playerName
          listColumns.field = "p" + i
          newColumns[newColumns.length] = listColumns
        }
        this.gridOptions.columns = newColumns
        this.gridOptions.data = newDatasSpan
        let lastTableFilter = {}
        lastTableFilter.name = '评价'
        lastTableFilter.fraction = memo
        if (lastTableFilter.fraction !== '') {
          tableDataFilter[tableDataFilter.length] = lastTableFilter
        }
        this.tableData = tableDataFilter
        this.complete = true
      }

    },
    created() {
      this.render()
    },
  }
</script>

<style scoped>

</style>
